package com.example.ylgh.controller.api;

import com.example.exception.YyghException;
import com.example.helper.HttpRequestHelper;
import com.example.result.Result;
import com.example.result.ResultCodeEnum;
import com.example.utils.MD5;
import com.example.ylgh.model.hosp.Department;
import com.example.ylgh.model.hosp.Hospital;
import com.example.ylgh.model.hosp.Schedule;
import com.example.ylgh.service.DepartmentService;
import com.example.ylgh.service.HospitalService;
import com.example.ylgh.service.HospitalSetService;
import com.example.ylgh.service.ScheduleService;
import com.example.ylgh.vo.hosp.DepartmentQueryVo;
import com.example.ylgh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author hx
 * @create 2022/5/11 10:02
 * description:
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/schedule/remove")
    public Result removeSche(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //获取医院和排班编号
        String hoscode = (String)map.get("hoscode");
        String hosScheduleId = (String)map.get("hosScheduleId");
        SignatureVerification(request);
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    /**
     * 查询排版
     * @param request
     * @return
     */
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //医院编号
        String hoscode = (String)map.get("hoscode");
        //科室编号
        String depcode = (String)map.get("depcode");

        //当前页和记录数
        int page = StringUtils.isEmpty(map.get("page"))?1: Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit"))?1: Integer.parseInt((String)map.get("limit"));
        //签名校验
        SignatureVerification(request);
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        //调用service方法
        Page<Schedule> pageDepartment =scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageDepartment);
    }

    /**
     * 上传排班接口
     * @param request
     * @return
     */
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //签名认证
        SignatureVerification(request);
        //调用service方法
        scheduleService.save(map);
        return Result.ok();
    }

    /**
     * 删除科室
     * @param request
     * @return
     */
    @PostMapping("/department/remove")
    public Result removeDep(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        String hoscode = (String)map.get("hoscode");
        String depcode = (String)map.get("depcode");
        //签名校验
        SignatureVerification(request);
        //调用service方法
        departmentService.remove(hoscode,depcode);
        return Result.ok();

    }

    /**
     * 查询科室接口
     * @param request
     * @return
     */
    @PostMapping("/department/list")
    public Result findDep(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        String hoscode = (String)map.get("hoscode");

        //当前页和记录数
        int page = StringUtils.isEmpty(map.get("page"))?1: Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit"))?1: Integer.parseInt((String)map.get("limit"));
        //签名校验
        SignatureVerification(request);
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageDepartment =departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageDepartment);

    }

    private void SignatureVerification(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)map.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String)map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)map.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String)map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法
        departmentService.save(map);
        return Result.ok();
    }

    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)map.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String)map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)map.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        map.put("logoData",logoData);
        //调用service方法
        hospitalService.save(map);
        return Result.ok();
    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> resultMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(resultMap);
        //获取医院编号
        String hoscode = (String) map.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)map.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法实现医院编号查询
        Hospital hospital=hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
}
